package com.yeqifu.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeqifu.bus.entity.Inport;
import com.yeqifu.bus.service.IInportService;
import com.yeqifu.bus.vo.InportVo;
import com.yeqifu.sys.common.*;
import com.yeqifu.sys.entity.Dept;
import com.yeqifu.sys.entity.Shelf;
import com.yeqifu.sys.entity.User;
import com.yeqifu.sys.service.IShelfService;
import com.yeqifu.sys.vo.DeptVo;
import com.yeqifu.sys.vo.ShelfVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@RequestMapping("/shelf")
public class ShelfController {
    @Autowired
    private IShelfService shelfService;
    private IInportService inportService;
    /**
     * 加载货架左边的菜单树
     * @param shelfVo
     * @return
     */
    @RequestMapping("loadShelfManagerLeftTreeJson")
    public DataGridView loadManagerLeftTreeJson(ShelfVo shelfVo, HttpSession session) {
        //查询出当前管理的所有仓库，存放进list中
//        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq('1');
        User user = (User) WebUtils.getSession().getAttribute("user");


//        List<Shelf> list = shelfService.list();
        List<Shelf> listBydeptid = shelfService.listBydeptid(user.getDeptid());
        System.out.println("***********************************");
        System.out.println(listBydeptid);
        List<TreeNode> treeNodes = new ArrayList<>();
        //将部门放入treeNodes中，组装成json
        for (Shelf shelf : listBydeptid) {
            Boolean open = shelf.getOpen() == 1 ? true : false;
            treeNodes.add(new TreeNode(shelf.getId(), shelf.getPid(), shelf.getRegion(), open));
        }
        return new DataGridView(treeNodes);
    }
    /**
     * 查询所有货架信息
     * @param shelfVo
     * @return
     */
    @RequestMapping("loadAllShelf")
    public DataGridView loadAllShelf(ShelfVo shelfVo){
        IPage<Shelf> page = new Page<>(shelfVo.getPage(),shelfVo.getLimit());
        //进行模糊查询
        QueryWrapper<Shelf> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(shelfVo.getRegion()),"region",shelfVo.getRegion());
        queryWrapper.like(StringUtils.isNotBlank(shelfVo.getType()),"type",shelfVo.getType());

        queryWrapper.and(wrapper -> wrapper.eq("id",shelfVo.getId()).or().eq("pid",shelfVo.getId()));
//        queryWrapper.eq(shelfVo.getId()!=null,"id",shelfVo.getId()).or().eq(shelfVo.getId()!=null,"pid",shelfVo.getId());

        //如果id等于空，直接返回
        if(shelfVo.getId()==null){
            return  new DataGridView(page.getTotal(),page.getRecords());
        }
        queryWrapper.orderByAsc("ordernum");
        //进行查询
        shelfService.page(page,queryWrapper);
        //返回DataGridView
        return new DataGridView(page.getTotal(),page.getRecords());
    }
    /**
     * 添加货架
     * @param shelfVo
     * @return
     */
    @RequestMapping("addshelf")
    public ResultObj addDept(ShelfVo shelfVo){
        try {
            if (shelfVo.getPid() == null) {
                shelfVo.setPid(0);
            }
            shelfService.save(shelfVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 加载排序码
     * @return
     */
    @RequestMapping("loadShelfMaxOrderNum")
    public Map<String,Object> loadDeptMaxOrderNum(){
        Map<String,Object> map = new HashMap<String,Object>();
        QueryWrapper<Shelf> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        IPage<Shelf> page = new Page<>(1,1);
        List<Shelf> list = shelfService.page(page,queryWrapper).getRecords();
        if (list.size()>0){
            map.put("value",list.get(0).getOrdernum()+1);
        }else {
            map.put("value",1);
        }
        return map;
    }
    /**
     * 更新货架
     * @param shelfVo
     * @return
     */
    @RequestMapping("updateShelf")
    public ResultObj updateDept(ShelfVo shelfVo){
        try {
            shelfService.updateById(shelfVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }
    /**
     * 检查当前货架是否有子货架
     * @param shelfVo
     * @return
     */
    @RequestMapping("checkShelfHasChildrenNode")
    public Map<String,Object> checkDeptHasChildrenNode(ShelfVo shelfVo){
        Map<String,Object> map = new HashMap<String, Object>();
        QueryWrapper<Shelf> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",shelfVo.getId());
        List<Shelf> list = shelfService.list(queryWrapper);
        if (list.size()>0){
            map.put("value",true);
        }else {
            map.put("value",false);
        }
        return map;
    }
    /**
     * 删除货架
     * @param shelfVo
     * @return
     */
    @RequestMapping("deleteShelf")
    public ResultObj deleteDept(ShelfVo shelfVo){
        try {
            shelfService.removeById(shelfVo.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 根据仓库Id查询货架
     *
     */
    @RequestMapping("loadShelfByWarehouseId")
    public DataGridView loadShelfByWarehouseId(Integer wareHouseName) {
            QueryWrapper<Shelf> queryWrapper = new QueryWrapper<>();
            queryWrapper
                    .eq("available", Constast.AVAILABLE_TRUE)
                    .eq("wmsid",wareHouseName);
            List<Shelf> list = shelfService.list(queryWrapper);
            for (Shelf shelf : list) {
                if (shelf.getCapacity()!=null)
                    shelf.setTotalCapacity(shelf.getTotal()-shelf.getCapacity());
                else
                    shelf.setTotalCapacity(shelf.getTotal());
            }
            return new DataGridView(list);
    }
}
